package com.code.leetcode._202504;
/**实现strStr**/
public class StrString {
    public int strStr(String haystack, String needle) {
        int n = haystack.length();
        int m = needle.length();
        char[] haystackChars = haystack.toCharArray();
        char[] needleChars = needle.toCharArray();
        //枚举原串的发起点
        for (int i = 0; i < n-m; i++) {
            //从原串的发起点和匹配串的首位开始尝试匹配
            int a = i;
            int b = 0;
            while (b < m && haystackChars[a] == needleChars[b]) {
                a ++;
                b ++;
            }
            //如果能够完全匹配，返回原串的发起点下标
            if (b == m){
                return i;
            }
        }
        return -1;
    }
}
